package com.onlinevideo.dao.impl;

import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.dao.VideoDao;
import com.onlinevideo.entity.Video;
import com.onlinevideo.util.PageUtils;
import com.onlinevideo.util.SQLUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VideoDaoImpl extends BaseDao implements VideoDao {
    @Override
    public int insert(Video video) {
        String sql = "INSERT INTO `T_video` (`id`,`user_id`,`category_id`,`video_name`,`upload_time`,`video_description`,`video_img`,`video_status`,`video_authority`,`play_total_count`,`collection_count`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
        return super.update(sql, video.getId(), video.getUserId(), video.getCategoryId(), video.getVideoName(), video.getUploadTime(), video.getVideoDescription(), video.getVideoImg(), video.getVideoStatus(), video.getVideoAuthority(), video.getPlayTotalCount(), video.getCollectionCount());
    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_video` WHERE `id`=?";
        return super.update(sql, id);
    }

    @Override
    public int update(Video video) {
        String sql = "UPDATE `T_video` SET `user_id`=?,`category_id`=?,`video_name`=?,`upload_time`=?,`video_description`=?,`video_img`=?,`video_status`=?,`video_authority`=?,`play_total_count`=?,`collection_count`=? WHERE `id`=?";
        return super.update(sql, video.getUserId(), video.getCategoryId(), video.getVideoName(), video.getUploadTime(), video.getVideoDescription(), video.getVideoImg(), video.getVideoStatus(), video.getVideoAuthority(), video.getPlayTotalCount(), video.getCollectionCount(), video.getId());
    }

    @Override
    public Video queryById(Long id) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `id`=? ";
        return super.queryForOne(Video.class, sql, id);
    }

    @Override
    public Video queryByIdS1(Long id) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `id`=? AND `video_status`=1";
        return super.queryForOne(Video.class, sql, id);
    }

    @Override
    public Video queryByIdS0(Long id) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `id`=? AND `video_status`=0";
        return super.queryForOne(Video.class, sql, id);
    }

    @Override
    public List<Video> queryAll() {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 ";
        return super.queryForList(Video.class, sql);
    }


    @Override
    public List<Video> queryAllS1() {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `video_status`=1";
        return super.queryForList(Video.class, sql);
    }

    @Override
    public List<Video> queryAllS0() {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `video_status`=0";
        return super.queryForList(Video.class, sql);
    }

    @Override
    public List<Video> queryByName(String name) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE video_name=? ";
        return queryForList(Video.class, sql, name);
    }

    @Override
    public List<Video> queryByConditions(Video video) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `id`=? AND `user_id`=? AND `category_id`=? AND `video_name`=? AND `upload_time`=? AND `video_description`=? AND `video_img`=? AND `video_status`=? AND `video_authority`=? AND `play_total_count`=? AND `collection_count`=?";
        Map map = SQLUtils.sqlValueMapping(sql, video);
        sql = (String) map.get("sql");
        Object[] data = (Object[]) map.get("data");
        return super.queryForList(Video.class, sql, data);
    }

    @Override
    public List<Video> queryForPageItems(int begin, int length) {
        return null;
    }

    @Override
    public Integer queryForPageTotalCount() {
        return null;
    }

    @Override
    public List<Video> queryForPageItems(int begin, int length, Video video) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` FROM `T_video` WHERE 1=1 AND `id`=? AND `user_id`=? AND `category_id`=? AND `video_name`=? AND `upload_time`=? AND `video_description`=? AND `video_img`=? AND `video_status`=? AND `video_authority`=? AND `play_total_count`=? AND `collection_count`=?";
        Map map = SQLUtils.sqlValueMapping(sql, video);
        List<Video> videos = super.queryForList(Video.class, String.valueOf(map.get("sql")), (Object[]) map.get("data"));
        return PageUtils.limit(begin, length, videos);
    }

    @Override
    public Integer queryForPageTotalCount(Video video) {
        String sql = "SELECT count(1) FROM `T_user` WHERE 1=1 AND `id`=? AND `username`=? AND `password`=? AND `email`=? AND `phone`=? AND `nickname`=? AND `role_id`=? AND `user_status`=? AND `user_validity`=? AND `logo_url`=? AND `create_time`=?";
        Map map = SQLUtils.sqlValueMapping(sql, video);
        Number number = (Number) super.queryForSingleValue(String.valueOf(map.get("sql")), (Object[]) map.get("data"));
        return number.intValue();
    }

    @Override
    public Map<String, Object> queryByPageS1(String category, String name, Integer pageNo) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (category == null) {
            category = "%";
        }
        if (name == null) {
            name = "%";
        }
        int pageSize = 24;
        int begin = (pageNo - 1) * pageSize;
        String sql = null;
        String trim = name.trim();
        if (category.matches("[0-9]+")) {
            sql = "select `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` from t_video where 1=1 and category_id =" + category + " and video_name like '%" + name.trim() + "%' AND `video_status`=1 limit " + begin + "," + pageSize;
        } else {
            sql = "select `id`,`user_id` AS `userId`,`category_id` AS `categoryId`,`video_name` AS `videoName`,`upload_time` AS `uploadTime`,`video_description` AS `videoDescription`,`video_img` AS `videoImg`,`video_status` AS `videoStatus`,`video_authority` AS `videoAuthority`,`play_total_count` AS `playTotalCount`,`collection_count` AS `collectionCount` from t_video where 1=1 and video_name like '%" + name.trim() + "%' AND `video_status`=1 limit " + begin + "," + pageSize;
        }
        List<Video> videos = super.queryForList(Video.class, sql);
        HashMap<String, Object> data = new HashMap<>();
        data.put("videos", videos);
        if (category.matches("[0-9]+")) {
            sql = "select count(1) from t_video where 1=1 and category_id=" + category + " and video_name like '%" + name.trim() + "%' AND `video_status`=1";
        } else {
            sql = "select count(1) from t_video where 1=1 and video_name like '%" + name.trim() + "%' AND `video_status`=1";
        }
        Number number = (Number) super.queryForSingleValue(sql);
        if (number == null) {
            number = 0;
        }
        Integer count = number.intValue();
        data.put("count", count);
        data.put("pageNo", pageNo);
        data.put("category", category);
        data.put("pageSize", pageSize);
        data.put("videoName", name);
        return data;
    }
}
